/***************************************************************************
 *   Copyright (C) 2009 by Marian Skvarek   *
 *   skvarek.marian@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A TrackICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "CTrack.h"
#include "CTrackView.h"
#include "CPart.h"
#include "CBar.h"
#include "CSymbol.h"

namespace easyscore
{
	namespace core
	{
//Constructors, Initialization & Destructors
		CTrack::CTrack(CPart *parentPart)
		{
			mParentPart = parentPart;
			mInitialized = false;

                        mID = 0;
			
			mView = 0;
		}

                void CTrack::initialize(bool addOneBar)
		{
			clear();

                        if(addOneBar)
                            addBar();

			mInitialized = true;
		}

		void CTrack::clear()
		{
			if ( !mInitialized )
				return;

			mInitialized = false;

			foreach ( CBar *bar, mBars )
			{
				delete bar;
			}
			mBars.clear();

			destroyView();
		}

		CTrack::~CTrack()
		{
			clear();
		}

                CBar *CTrack::addBar()
                {
                    CBar *newBar = new CBar ( this );
                        addBar ( newBar );
                        return newBar;
                }

                void CTrack::addBar ( CBar *bar )
                {
                        mBars.append ( bar );

                        updateView();
                }

                void CTrack::removeBar( CBar *bar)
                {
                        mBars.removeOne(bar);
                        delete bar;

                        updateView();
                }

                QLinkedList<CSymbol *> CTrack::getSymbols()
                {
                    QLinkedList<CSymbol *> result;

                    foreach(CBar *bar, mBars)
                        result += bar->getSymbols();

                    return result;
                }

                void CTrack::setActiveBars(int start, int count, bool value)
                {
                    int current = 0;
                    foreach(CBar *bar, mBars)
                    {
                        if(current >= start && current < (start + count) )
                            bar->setActive(value);
                        else
                            bar->setActive(!value);

                        ++current;
                    }
                }



		/*!
		\fn easyscore::core::CTrack::createView()
		\brief creates a view
		*/
		void CTrack::createView()
		{
			destroyView();

			mView = new CTrackView ( this );
		}

		/*!
		\fn easyscore::core::CTrack::destroyView()
		\brief destroys a view
		 */
		void CTrack::destroyView()
		{
			foreach ( CBar *bar, mBars )
			{
				bar->destroyView();
			}

			if ( mView != 0 )
			{
				delete mView;
				mView = 0;
			}
		}
		
//Protected Methods
		void CTrack::updateView()
		{
			if(hasView())
				mView->update();
		}



	}//namespace core
}//namespace easyscore














